//
//  LCViewController.m
//  B项目期
//
//  Created by lanou3g on 16/3/21.
//  Copyright © 2016年 LC. All rights reserved.
//

#import "LCViewController.h"
#import "LCViewCell.h"
#import "SKModel.h"
#import "UIImageView+WebCache.h"
#import "CDHttpHelper.h"
#import "SDRefresh.h"

#import "TravelDetailTableViewController.h"
#import "LCImageViewController.h"


#import "TravelDetailTableViewController.h"
#import "Singleton.h"




@interface LCViewController ()
@property(nonatomic,strong)SDRefreshFooterView * refreshFooter;
@property (nonatomic, weak) SDRefreshHeaderView *refreshHeader;
@property (nonatomic, assign) NSInteger totalRowCount;

//@property (nonatomic,strong) SDRefreshHeaderView *refreshHeader;



@end

@implementation LCViewController
-(id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.title = @"上拉和下拉刷新";
        self.tableView.rowHeight = 30.0f;
        self.tableView.separatorColor = [UIColor whiteColor];
        _totalRowCount = 0;
    }
    return self;
}
-(NSMutableArray *)allDataArray
{
    if (!_allDataArray) {
        _allDataArray = [[NSMutableArray alloc]init];
    }
    return _allDataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[LCViewCell class] forCellReuseIdentifier:@"xxx"];

    [self setupHeader];
    [self setupFooter];
    
    [self handata];
}

- (void)setupHeader
{
    SDRefreshHeaderView *refreshHeader = [SDRefreshHeaderView refreshView];
    
    // 默认是在navigationController环境下，如果不是在此环境下，请设置 refreshHeader.isEffectedByNavigationController = NO;
    [refreshHeader addToScrollView:self.tableView];
    
    __weak SDRefreshHeaderView *weakRefreshHeader = refreshHeader;
    __weak typeof(self) weakSelf = self;
    refreshHeader.beginRefreshingOperation = ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.totalRowCount += 3;
            [weakSelf.tableView reloadData];
            [weakRefreshHeader endRefreshing];
        });
    };
    
    // 进入页面自动加载一次数据
    [refreshHeader autoRefreshWhenViewDidAppear];
}

-(void)setupFooter
{
    self.totalRowCount +=1;
    [self.tableView reloadData];
    
    self.refreshFooter = [SDRefreshFooterView refreshView];
    [self.refreshFooter addToScrollView:self.tableView];
    [self.refreshFooter addTarget:self refreshAction:@selector(footerRefresh)];
    _refreshFooter = self.refreshFooter;
    
}
- (void)footerRefresh
{
    self.totalRowCount +=1;
    [self handata];
}
-(void)handata
{
   
    [[CDHttpHelper defaultCDHttpHelper]get:[NSString stringWithFormat:@"https://chanyouji.com/api/trips/featured.json?page=%ld",self.totalRowCount]params:nil success:^(id responseObj) {
        NSError *error = nil;
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:&error];
    
        for (NSDictionary *dic in array) {
            SKModel *model = [[SKModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allDataArray addObject:model];
        }
        [self.tableView reloadData];
//        SDRefreshHeaderView *refreshHeader = [SDRefreshHeaderView refreshView];
        [self.refreshFooter endRefreshing];

    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];

    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


}


#pragma mark - Table view data source
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       return self.allDataArray.count;
;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xxx" forIndexPath:indexPath];

    
    SKModel *skmo = [[SKModel alloc]init];
    skmo = self.allDataArray[indexPath.row];
    cell.aLabel.text = skmo.name;
    cell.cLabel.text = [NSString stringWithFormat:@"%ld图",skmo.photos_count];
    cell.bLabel.text = [NSString stringWithFormat:@"%@ / %ld天",skmo.start_date,skmo.days];
    [cell.aimageView sd_setImageWithURL:[NSURL URLWithString:skmo.front_cover_photo_url]];

      [cell.bImageView sd_setImageWithURL:[NSURL URLWithString:skmo.user[@"image"]]];
    
    [cell.aButton addTarget:self action:@selector(pushAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    

    [cell.bImageView sd_setImageWithURL:[NSURL URLWithString:skmo.user[@"image"]]];

    return cell;
}
-(void)pushAction
{
    LCImageViewController *t = [LCImageViewController new];
    
    [self.navigationController pushViewController:t animated:YES];
    
    
    
}

 进入详情

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SKModel *skModel = self.allDataArray[indexPath.row];
    TravelDetailTableViewController *tra = [TravelDetailTableViewController new];
    tra.detailModel = skModel;
    [self.navigationController pushViewController:tra animated:YES];
    TravelDetailTableViewController *travelDetailsVC = [[TravelDetailTableViewController alloc]init];
    
    SKModel *model = [[SKModel alloc]init];
    model = self.allDataArray[indexPath.row];
    
    Singleton *single = [Singleton shareSingle];
    single.ID = model.ID;
    
    [self.navigationController pushViewController:travelDetailsVC animated:YES];
}



// 进入详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SKModel *skModel = self.allDataArray[indexPath.row];
    TravelDetailTableViewController *tra = [TravelDetailTableViewController new];
    tra.detailModel = skModel;
    [self.navigationController pushViewController:tra animated:YES];
    TravelDetailTableViewController *travelDetailsVC = [[TravelDetailTableViewController alloc]init];
    
    SKModel *model = [[SKModel alloc]init];
    model = self.allDataArray[indexPath.row];
    
    Singleton *single = [Singleton shareSingle];
    single.ID = model.ID;
    
    [self.navigationController pushViewController:travelDetailsVC animated:YES];
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    SKModel *skModel = self.allDataArray[indexPath.row];
//    TravelDetailTableViewController *tra = [TravelDetailTableViewController new];
//    tra.detailModel = skModel;
//    [self.navigationController pushViewController:tra animated:YES];
//    
//
//}


-(void)tableView:(UITableView *)tableView willDisplayCell:(LCViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    CATransform3D rotation;//3D旋转
    
    rotation = CATransform3DMakeTranslation(0 ,50 ,20);
    rotation = CATransform3DScale(rotation, 0.9, .9, 1);
    
    rotation.m34 = 1.0/ -600;
    
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    
    cell.layer.transform = rotation;
    
    [UIView beginAnimations:@"rotation" context:NULL];
    //旋转时间
    [UIView setAnimationDuration:0.6];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
}

@end
